package com.lemon.day012;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class JDBCUtil {
    private static Properties properties = new Properties();
    //静态代码块
    static{
        FileInputStream inStream = null;
        try {
            inStream = new FileInputStream(new File("src/test/resources/jdbc.properties"));
            properties.load(inStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static Connection getConnection(String jdbcFilePath){

        try {
            String url = properties.getProperty("jdbc.url");
            String user = properties.getProperty("jdbc.username");
            String password = properties.getProperty("jdbc.password");
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    public static Map<String,Object> query(String sql,Object...values) {
        Connection connection = JDBCUtil.getConnection("src/test/resources/jdbc.properties");
        Map<String, Object> map = null;
        try {
            // 2.通过connection连接对象去获取一个prepareStatement类型的对象
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 3.给条件字段赋值
            if(values!=null){
                int valueCount = values.length;
                for (int i = 1; i <=valueCount ; i++) {
                    preparedStatement.setObject(i,values[i-1]);
                }
            }

            // 4.执行数据库操作
            ResultSet resultSet = null;
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();
            // 5.从结果集中取数据
            map = new HashMap<String, Object>();
            while (resultSet.next()) {
                for (int i = 1; i < columnCount; i++) {
                    String columnLabel=resultSetMetaData.getColumnLabel(i);
                    Object columnValue = resultSet.getObject(columnLabel);
                    map.put(columnLabel, columnValue);
                }
/*                String label1 = "id";
                String label2 = "regname";
                Object columnValue1 = resultSet.getObject(label1);
                Object columnValue2 = resultSet.getObject(label2);
                map.put(label1, columnValue1);
                map.put(label2, columnValue2);*/
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return map;
    }
}
